package sg.edu.nus.iss.billing.model.bill;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import sg.edu.nus.iss.billing.utils.DateHelper;

/**
 * @author ZiFeng, YinNi, Thurien
 */
public class BillingPeriod {
	BillMonth billingMonth;
	int billingYear;
	Calendar c;

	public BillingPeriod(BillMonth billingMonth, int billingYear) {
		this.billingMonth = billingMonth;
		this.billingYear = billingYear;
		this.c = Calendar.getInstance();
		
		this.c.set(Calendar.YEAR, this.getBillingYear());
		// because of the zero based need to substract 1
		this.c.set(Calendar.MONTH, this.getBillingMonth().getNumber() - 1);
	}

	public BillMonth getBillingMonth() {
		return this.billingMonth;
	}

	public int getBillingYear() {
		return this.billingYear;
	}

	public Date getEndDate() {
		int date = this.c.getActualMaximum(Calendar.DATE);
		this.c.set(Calendar.DATE, date);
		return formatDateTime(this.c.getTime());
	}

	public Date getStartDate() {
		int date = this.c.getActualMinimum(Calendar.DATE);
		this.c.set(Calendar.DATE, date);
		return formatDateTime(this.c.getTime());
	}

	public long getNumberOfDays() {
		return DateHelper.getDaysInBetween(getStartDate(), getEndDate());
	}
	
	public Date formatDateTime(Date inputDate) {
		Date formattedDate = null;
		DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		String strDate = sdf.format(inputDate);
		
		try {
			formattedDate = sdf.parse(strDate);
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return formattedDate;
	}
}
